<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Log;
/**
 * Class AdminGenerate
 * @package App\Console\Commands
 * 
 *   php artisan command:sql
 */

class SqlCreate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:sql';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description：generate sql File';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handletest()
    {
        //
        $columns = Schema::getColumnListing('resumes');

//        Schema::getFacadeRoot();
//        $columns = array_flip($columns);
//        $columns = array_fill(0,35,"");
        $keys = array_values($columns);
        $a = array_fill_keys($keys, '');
        var_dump($a);
//        dd($a);

        $connection = DB::connection();
        $connection->getSchemaBuilder();

        $table   = "resumes";
        $grammar = $connection->getSchemaGrammar();
        $results = $connection->select($grammar->compileColumnExists(), array($connection->getDatabaseName(), $table));
//table_name()
        $results =  $connection->getPostProcessor()->processColumnListing($results);

        dd($results);

        $columns = DB::connection()
            ->getDoctrineSchemaManager()
            ->listTableColumns('resumes');
dd($columns);
        foreach($columns as $column) {
            print $column->getName();
            print $column->getType()->getName();
            print $column->getDefault();
            print $column->getLength();
        }


/*        Below is a list of all the available column modifiers. This list does not include the index modifiers:
Modifier
Description
->after('column')
Place the column "after" another column (MySQL Only)
->comment('my comment')
Add a comment to a column
->default($value)
Specify a "default" value for the column
->first()
Place the column "first" in the table (MySQL Only)
->nullable()
Allow NULL values to be inserted into the column
->storedAs($expression)
Create a stored generated column (MySQL Only)
->unsigned()
Set integer columns to UNSIGNED
->virtualAs($expression)
Create a virtual generated column (MySQL Only)*/
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
//    public function AddColum()
    public function handle()
    {
        $database = 'mbrswkj';
        $connection = DB::connection();
        Config::set('database.connections.mysql.database', $database);
        DB::reconnect('mysql');

        $connection->getSchemaBuilder();
        $connection = $connection->getDoctrineConnection();
        //增加自定义格式内容
        $connection->getDatabasePlatform()->registerDoctrineTypeMapping('json', 'text');
        $connection->getDatabasePlatform()->registerDoctrineTypeMapping('jsonb', 'text');
        $connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
        $connection->getDatabasePlatform()->registerDoctrineTypeMapping('bit', 'boolean');
        $this->schema = $connection->getSchemaManager();
        $tableNames = $this->schema->listTableNames();

//        return $tableNames;
//        $result = DB::select('SHOW TABLES');
        var_dump( $tableNames);

        $i = 0;
//        $old_prefix = "user_";
//        $old_prefix = "edu_";
        //过滤要修改前缀的表名
//        foreach ($tableNames as $k => $v) {
//            $preg = preg_match("/^($old_prefix{1})([a-zA-Z0-9_-]+)/i", $v, $v1);
//            if ($preg) {
////                $tab_name[$k] = $v1[2];
//                $tab_name[$k] = $v1[0];
//            }
//        }
//        return $tab_name;

//        $columns = Schema::getColumnListing('users');
//        dd($columns);

        foreach ($tableNames as $value) {
            Log::info($value);

            Log::info(json_encode($value));
            $columns = $this->schema->listTableColumns($value,$database);
//            $columns = $this->schema->getColumnListing($value);
            Log::info(json_encode($columns));
//            var_dump($columns);

//            exit;
            if (!isset($columns['created_at'])) {
                Schema::table($value, function ($table) {
                    $table->timestamp('created_at')->nullable();
                });
            }

            if (!isset($columns['updated_at'])) {
                Schema::table($value, function ($table) {
                    $table->timestamp('updated_at')->nullable();
                });
            }
            if (!isset($columns['deleted_at'])) {
                Schema::table($value, function ($table) {
                    $table->timestamp('deleted_at')->nullable();
                });
            }

//            return $columns;
            $i++;

        }
//        Log::info("一共处理了 $i 条数据");
        return ("一共处理了 $i 条数据");
        return $result;
    }
}
